﻿using DreamWork.WebAPI;
using dreamwork.auth.api.Model.ViewModel;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace dreamwork.auth.api
{
    [Route("api/[controller]")]
	public class TokenController : DreamWorkControllerBase
	{
		private readonly ITokenService _tokenService;

		public TokenController(ITokenService tokenService)
		{
			_tokenService = tokenService;
		}

		[Route("[action]"), HttpPost,AllowAnonymous]
		public IActionResult Login([FromBody]LoginModel login)
		{
			var userId = Guid.NewGuid().ToString();
			return Success(new UserToken() { BearerToken = _tokenService.CreateToken(userId),UserId=userId });
		}

		[ Route("[action]"), HttpGet, Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
        public IActionResult ValidateToken()
		{
			return Success(CurrentUserId);
		}

		public class LoginModel
		{
			public string Username { get; set; }
			public string Password { get; set; }
		}

		private class UserModel
		{
			public string Id { get; set; }
			public string Name { get; set; }
			public string Email { get; set; }
			public DateTime Birthdate { get; set; }
		}
	}
}
